package cn.kgc.springcloud.service.impl;

import cn.kgc.springcloud.client.CityClient;
import cn.kgc.springcloud.client.ClazzClient;
import cn.kgc.springcloud.client.GroupClient;
import cn.kgc.springcloud.client.TagClient;
import cn.kgc.springcloud.entity.*;
import cn.kgc.springcloud.form.StudentForm;
import cn.kgc.springcloud.mapper.StudentMapper;
import cn.kgc.springcloud.mapper.StudentTagMapper;
import cn.kgc.springcloud.service.StudentService;
import cn.kgc.springcloud.utils.DateUtil;
import cn.kgc.springcloud.vo.StudentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService{

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private StudentTagMapper  studentTagMapper;

    @Autowired
    private CityClient cityClient;

    @Autowired
    private ClazzClient clazzClient;

    @Autowired
    private GroupClient groupClient;

    @Autowired
    private TagClient tagClient;

    @Override
    public List<StudentVO> getStudentList() {
        //查询学生信息列表  student表
        List<Student> students = studentMapper.selectList(null);
        //遍历学生列表   student ->studentVO
        List<StudentVO> collect = students.stream().map(student -> {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student,studentVO);

            // 城市信息   city服务
            Integer cityid = student.getCityid();
            City city = cityClient.getById(cityid);
            studentVO.setCity(city);

            //班级信息   clazz服务
            Integer clazzid = student.getClazzid();
            Clazz clazz = clazzClient.getById(clazzid);
            studentVO.setClazz(clazz);

            // 小组信息  group服务
            Integer groupid = student.getGroupid();
            Group group = groupClient.getById(groupid);
            studentVO.setGroup(group);

            // 标签列表
            Integer studentId = student.getId();
            LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
            lambda.eq(StudentTag::getSid,studentId);

            List<StudentTag> studentTags = studentTagMapper.selectList(lambda);
            List<Integer> tIds = studentTags.stream().map(studentTag -> studentTag.getTid()).collect(Collectors.toList());

            // 向标签服务发送请求
            List<Tag> tags = tIds.stream().map(tid -> {
                Tag tag = tagClient.getTagById(tid);
                return tag;
            }).collect(Collectors.toList());

            studentVO.setTags(tags);
            return studentVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public StudentVO addStudent(StudentForm studentForm) {
        StudentVO studentVO = new StudentVO();
        // 添加学生信息
        Student student = new Student();
        BeanUtils.copyProperties(studentForm,student);
        // 获取学生生日  计算 学生的年龄 属相  星座
        Date bir = student.getBir();
        // 年龄
        int age = DateUtil.getAge(bir);
        student.setAge(age);
        //星座
        String constellation = DateUtil.getConstellation(bir);
        student.setStarts(constellation);
        //属相
        String year = DateUtil.getYear(bir);
        student.setAttr(year);

        //将学生信息写入student
        studentMapper.insert(student);

        //将学生的标签信息写入 student_tag
        Integer studentId = student.getId();
        List<Integer> tagIds = studentForm.getTagIds();

        tagIds.stream().forEach(tid->{
            StudentTag studentTag = new StudentTag();
            studentTag.setSid(studentId);
            studentTag.setTid(tid);
            studentTagMapper.insert(studentTag);
        });

        BeanUtils.copyProperties(student,studentVO);

        // 城市信息   city服务
        Integer cityid = student.getCityid();
        City city = cityClient.getById(cityid);
        studentVO.setCity(city);

        //班级信息   clazz服务
        Integer clazzid = student.getClazzid();
        Clazz clazz = clazzClient.getById(clazzid);
        studentVO.setClazz(clazz);

        // 小组信息  group服务
        Integer groupid = student.getGroupid();
        Group group = groupClient.getById(groupid);
        studentVO.setGroup(group);

        //根据学生id  查询标签详细信息
        LambdaQueryWrapper<StudentTag> lambda = new QueryWrapper<StudentTag>().lambda();
        lambda.eq(StudentTag::getSid,studentId);


        List<StudentTag> studentTags = studentTagMapper.selectList(lambda);
        List<Integer> tIds = studentTags.stream().map(studentTag -> studentTag.getTid()).collect(Collectors.toList());

        List<Tag> tags = tIds.stream().map(tid -> {
            Tag tag = tagClient.getTagById(tid);
            return tag;
        }).collect(Collectors.toList());

        studentVO.setTags(tags);

        return studentVO;
    }
}




